﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace MusicDownload
{
    public class UIChannel<T>
    {
        Channel<T> channel = new Channel<T>();

        public event Action<T> OnPopEvent;

        public UIChannel()
        {
            var context = SynchronizationContext.Current;

            new Thread(() =>
            {
                while (true)
                {
                    var data = channel.Pop();
                    context.Post((obj) => { OnPopEvent.Invoke((T)obj); }, data);
                }
            })
            {
                IsBackground = true
            }.Start();
        }

        public void Push(T data)
        {
            channel.Push(data);
        }
    }
}
